$(function () {
  chrome.storage.sync.get(['total', 'limit'], function (budget) {
    $("#total").text(budget.total);
    $("#limit").text(budget.limit);
  })
  $("#add").click(function () {
    // 1.从浏览器中获取存储金额
    chrome.storage.sync.get(['total', 'limit'], function (budget) {
      var totalAmount = 0;
      var limit = budget.limit;
      if (budget.total) {
        totalAmount = parseFloat(budget.total);
      }
      // 2.将本次金额加到总金额并存储
      var amount = $("#amount").val();
      if (amount) {
        if (limit >= (totalAmount + parseFloat(amount))) {
          totalAmount += parseFloat(amount);
          chrome.storage.sync.set({ total: totalAmount });
        } else {
          alert("超过限制金额")
        }
      }
      // 3.更新显示ui
      $("#total").text(totalAmount);
      $("#amount").val("");
    })
  })
})